package com.example.onlineeducation.repository

import com.example.onlineeducation.data.entity.*
import com.example.onlineeducation.mapper.ChapeterMapper
import com.example.onlineeducation.mapper.StudyPlanMapper
import com.example.onlineeducation.mapper.TopicMapper
import com.example.onlineeducation.mapper.*
import com.example.onlineeducation.net.ApiService
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.yc.jetpacklib.base.YcRepository
import com.yc.jetpacklib.extension.ycFlow
import com.yc.jetpacklib.extension.ycToRequestBody
import com.yc.jetpacklib.net.YcResult
import com.yc.jetpacklib.recycleView.YcPager
import kotlinx.coroutines.flow.Flow
import listToIMapper
import org.json.JSONException
import org.json.JSONObject
import pageListToDataSource
import ycGetDataNoNull

/**
 * SimpleDes:
 * Creator: Sindi
 * Date: 2022-04-15
 * UseDes:
 */
class StudyRepository(private val apiService: ApiService) : YcRepository() {

    /**
     * 积分明细列表
     */
    private val mScoreRecordListMapper by lazy {
        ScoreRecordListMapper()
    }

    fun getScoreRecordListPager() = YcPager.getPagerFlow { pageIndex, pageSize ->
        apiService.getScoreRecordList( pageIndex, pageSize).pageListToDataSource(mScoreRecordListMapper)
    }


    /**
     * 获取证书列表
     */
    private val mCertificateListMapper by lazy {
        CertificateListMapper()
    }

    fun getCertificateListPager(proId: String) = YcPager.getPagerFlow { pageIndex, pageSize ->
        apiService.getCertificateList(proId, pageIndex, pageSize).pageListToDataSource(mCertificateListMapper)
    }



    /**
     * 考试记录列表
     */
    private val mExamRecordListMapper by lazy {
        ExamRecordListMapper()
    }

    fun getExamRecordListPager(proId: String) = YcPager.getPagerFlow { pageIndex, pageSize ->
        apiService.getExamRecordList(proId, pageIndex, pageSize).pageListToDataSource(mExamRecordListMapper)
    }


    /**
     * 考试结果详情
     */
    private val mExamResultDetailMapper by lazy {
        ExamResultDetailMapper()
    }

    fun getExamResultDetail(examLogId: String): Flow<YcResult<ExamResultDetailEntity>> = ycFlow {
        val json = apiService.getExamResultDetail(examLogId).ycGetDataNoNull()
        send(YcResult.Success(mExamResultDetailMapper.map(json)))
    }


    /**
     * 获取学习素材列表
     */
    private val mChapeterMapper by lazy {
        ChapeterMapper()
    }

    fun getChapterList(projectId: String, topicId: String): Flow<YcResult<List<ChapterEntity>?>> = ycFlow {
        val json = apiService.getChapterList(projectId, topicId).listToIMapper(mChapeterMapper)
        send(YcResult.Success(json))
    }

    /**
     * 上传学习时间
     */
    fun pushStudyLog(projId: String, times: String, topicId: String): Flow<YcResult<Any?>> = ycFlow {
        val requestData = JSONObject()
        try {
            requestData.put("projId", projId)//项目id
            requestData.put("times", times)//学习时间
            requestData.put("topicId", topicId)//专题id
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        val requestBody = requestData.toString().ycToRequestBody()
        val json = apiService.pushStudyLog(requestBody)
        send(YcResult.Success(json))
    }


    /*
    * 学习计划列表
    * */
    private val mStudyPlanMapper by lazy {
        StudyPlanMapper()
    }

    fun getStudyPlanList(projectId: String): Flow<YcResult<List<StudyPlanEntity>?>> = ycFlow {
        val json = apiService.getStudyPlanList(projectId).listToIMapper(mStudyPlanMapper)
        send(YcResult.Success(json))
    }


    /**
     * 获取专题列表
     */
    private val mTopicMapper by lazy {
        TopicMapper()
    }

    fun getTopicList(projectId: String, groupId: String): Flow<YcResult<List<TopicEntity>?>> = ycFlow {
        val json = apiService.getTopicList(projId = projectId, groupId = groupId).listToIMapper(mTopicMapper)
        send(YcResult.Success(json))
    }

    /**
     * 获取错题列表
     */
    fun getErrorList(projectId: String): Flow<YcResult<List<TopicEntity>?>> =
        ycFlow {
            val json = apiService.getErrorList(projectId)
                .listToIMapper(mTopicMapper)
            send(YcResult.Success(json))
        }

    /**
     *  获取 练习-题目列表
     */
    private val mPractiseQuestionMapper by lazy {
        PractiseQuestionMapper()
    }

    fun getPractiseQuestionList(chapterId: String): Flow<YcResult<List<ProblemEntity>?>> = ycFlow {
        val json = apiService.getPractiseQuestionList(chapterId)
        send(YcResult.Success(mPractiseQuestionMapper.map(json.dataList)))
    }


    /**
     *  练习——提交答案
     */
    private val mSubmitPractiseOptionsMapper by lazy {
        SubmitPractiseOptionsMapper()
    }

    fun submitPractiseOptions(
        proId: String,
        topicId: String,
        chapterId: String,
        questionId: String,
        optionList: List<OptionEntity>
    ): Flow<YcResult<List<String>?>> = ycFlow {
        val requestData = JsonObject()
        try {
            requestData.addProperty("projId", proId)
            requestData.addProperty("topicId", topicId)
            requestData.addProperty("chapterId", chapterId)
            requestData.addProperty("questionId", questionId)
            val optionIdArray = JsonArray()
            optionList.map {
                optionIdArray.add(it.optionId)
            }
            requestData.add("optionIds", optionIdArray)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        val requestBody = requestData.toString().ycToRequestBody()
        val json = apiService.submitPractiseOptions(requestBody)
        send(YcResult.Success(mSubmitPractiseOptionsMapper.map(json.dataList)))
    }
    private val mUserProStudyTimeMapper by lazy { UserProStudyTimeMapper() }

    /**
     * 获取用户在x项目的学习时长
     */
    fun getUserProStudyTime(proId: String): Flow<YcResult<UserProStudyTimeEntity>> = ycFlow {
        val json = apiService.getUserProStudyTime(proId)
        send(YcResult.Success(mUserProStudyTimeMapper.map(json.data)))
    }

    private val mUserProStudyChartMapper by lazy { UserProStudyChartMapper() }

    /**
     * 获取用户在x项目的学习情况图表数据
     */
    fun getUserProStudyChar(proId: String, timeStart: String, timeEnd: String): Flow<YcResult<UserProStudyChartEntity>> = ycFlow {
        val json = apiService.getUserProStudyChar(proId, timeStart, timeEnd)
        send(YcResult.Success(mUserProStudyChartMapper.map(json.data)))
    }


    /**
     * 在线考试扫一扫
     */
    private val mExamInfoAndTopicInfoMapper by lazy {
        ExamAndTopicMapper()
    }

    fun getExamInfoAndTopicInfo(examId: String,topicId: String,projId: String): Flow<YcResult<ExamAndTopicEntity>> = ycFlow {
        val json = apiService.getExamInfoAndTopicInfo(examId,topicId,projId).ycGetDataNoNull()
        send(YcResult.Success(mExamInfoAndTopicInfoMapper.map(json)))
    }

}